/**
 * 
 */
package de.cbf.cam.dsa.exchange.heldenrmi.views;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import de.cbf.cam.dsa.exchange.heldenrmi.composites.CompositeImportRMI;
import de.cbf.cam.dsa.exchange.heldenrmi.descriptions.CharacterDescription;

/**
 * @author Stefan Alter
 *
 */
public class ImportRMIView extends ViewPart {

	public static final String ID = "de.cbf.cam.dsa.exchange.heldenrmi.views.ImportRMIView";
	private CompositeImportRMI compositeImport;

	/**
	 * 
	 */
	public ImportRMIView() {	
		
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		compositeImport = new CompositeImportRMI(parent, SWT.NONE);		
		compositeImport.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		compositeImport.registerImportListener(new ImportListener());
		compositeImport.registerReloadListener(new ReloadListener());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
	}

	public void fillCharacters(List<CharacterDescription> characterDescriptions){
		compositeImport.fillCharacters(characterDescriptions);
	}
	
	public CharacterDescription getSelectedCharacterDescription() {
		return compositeImport.getSelectedDescription();
	}
	
	private class ImportListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			ActionEvent event = new ActionEvent(ImportRMIView.this, ActionEvent.ACTION_PERFORMED,"Import");
			for (ActionListener listener : importListeners) {
				listener.actionPerformed(event);
			}			
		}		
	}
	private List<ActionListener> importListeners = new ArrayList<ActionListener>();
	public boolean registerImportListener(ActionListener listener){
		return importListeners.add(listener);		
	}
	public boolean deRegisterImportListener(ActionListener listener){
		return importListeners.remove(listener);
	}
	
	private class ReloadListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			ActionEvent event = new ActionEvent(ImportRMIView.this, ActionEvent.ACTION_PERFORMED,"Reload");
			for (ActionListener listener : reloadListeners) {
				listener.actionPerformed(event);
			}			
		}		
	}
	private List<ActionListener> reloadListeners = new ArrayList<ActionListener>();
	public boolean registerReloadListener(ActionListener listener){
		return reloadListeners.add(listener);		
	}
	public boolean deRegisterReloadListener(ActionListener listener){
		return reloadListeners.remove(listener);
	}

	
}
